/**
 * 
 */
package com.auxgroup.hr.front.commons.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.apache.shiro.authz.Permission;
import org.springframework.util.CollectionUtils;


/**
 * @author ye.meng@ssitsh.com
 * @since 1.0.0 create date 2017年10月16日
 * shiro的用户信息管理类
 */
public class AuthenticatedPrincipal implements AuxSecurityPrincipal {

	private static final long serialVersionUID = 1L;

	private String userId;//用户ID

	private Collection<String> roles=new HashSet<String>();//角色
	private Collection<String> groups=new HashSet<String>();//角色组

	private boolean adminPrincipal;//是否admin

	/*
	 * Extra attributes, depending on the authentication implementation. Note
	 * that we only support String - String attributes as we need to be able to
	 * persist the Principal generically
	 */
	private Map<String, String> attributes;

	public AuthenticatedPrincipal() {
		super();
	}

	public AuthenticatedPrincipal(String userId) {
		this(userId, new ArrayList<String>());
	}

	public AuthenticatedPrincipal(String userId, Collection<String> roles) {
		this(userId, roles, new HashMap<String, String>());
	}

	public AuthenticatedPrincipal(String userId, Collection<String> roles,
			Map<String, String> attributes) {
		this(userId, roles, attributes, new ArrayList<String>());
	}

	public AuthenticatedPrincipal(String userId, Collection<String> roles,
			Map<String, String> attributes, Collection<String> groups) {
		this(userId, roles, attributes, groups, false);
	}

	public AuthenticatedPrincipal(String userId, Collection<String> roles,
			Map<String, String> attributes, Collection<String> groups,
			boolean adminPrincipal) {
		this.userId = userId;
		this.roles = roles;
		this.attributes = attributes;
		this.groups = groups;
		this.adminPrincipal = adminPrincipal;
	}

	/**
	 * @return the roles
	 */
	public Collection<String> getRoles() {
		return roles;
	}

	/**
	 * @return the attributes
	 */
	public Map<String, String> getAttributes() {
		return attributes;
	}

	/**
	 * Get the given attribute.
	 * 
	 * @param key
	 *            the attribute key to get.
	 * @return String value if attribute found. Null if attribute not found or
	 *         no attributes at all.
	 */
	public String getAttribute(String key) {
		if (attributes == null) {
			return null;
		}
		return attributes.get(key);
	}

	public void addAttribute(String key, String value) {
		if (attributes == null) {
			attributes = new HashMap<String, String>();
		}
		attributes.put(key, value);
	}

	public void addGroup(String name) {
		if (groups == null) {
			groups = new ArrayList<String>();
		}
		groups.add(name);
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	public long getUserIdAsLong(){
		return Long.parseLong(userId);
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return getClass().getName() + " [userId=" + userId + ", roles=" + roles
				+ ", attributes=" + attributes + "]";
	}

	/**
	 * @param roles
	 *            the roles to set
	 */
	public void setRoles(Collection<String> roles) {
		this.roles = roles;
	}

	/**
	 * @param attributes
	 *            the attributes to set
	 */
	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}

	public Collection<String> getGroups() {
		return groups;
	}

	public void setGroups(Collection<String> groups) {
		this.groups = groups;
	}

	public boolean isGroupAware() {
		return !CollectionUtils.isEmpty(groups);
	}

	public boolean isAdminPrincipal() {
		return adminPrincipal;
	}

	public void setAdminPrincipal(boolean adminPrincipal) {
		this.adminPrincipal = adminPrincipal;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.security.Principal#getName()
	 */
	@Override
	public String getName() {
		return this.getUserId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.shiro.authz.AuthorizationInfo#getStringPermissions()
	 */
	@Override
	public Collection<String> getStringPermissions() {
		return new HashSet<String>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.shiro.authz.AuthorizationInfo#getObjectPermissions()
	 */
	@Override
	public Collection<Permission> getObjectPermissions() {
		return new HashSet<Permission>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * #getDisplayName()
	 */
	@Override
	public String getDisplayName() {
		return this.getAttribute("realName");
	}

	public String getMobileNum() {
		return this.getAttribute("mobileNum");
	}

}
